﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AnyExeTo.WindowsService
{
    /// <summary>
    /// 服务选项
    /// </summary>
    public partial class FormServiceOptions : Form
    {
        /// <summary>
        /// 初始化服务选项
        /// </summary>
        public FormServiceOptions()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormServiceOptions_Load(object sender, EventArgs e)
        {
            LoadDataFromFile();
        }

        /// <summary>
        /// 从文件加载数据
        /// </summary>
        private void LoadDataFromFile()
        {
            var options = ServiceOptions.LoadFromFile();
            textBoxServiceName.Text = options.ServiceName;
            textBoxDisplayName.Text = options.DisplayName;
            textBoxDescription.Text = options.Description;
            comboBoxStartType.Text = options.StartType;
            comboBoxAccount.Text = options.Account;
            textBoxExeFileName.Text = options.ExeFileName;
            textBoxStartArguments.Text = options.StartArguments;
            textBoxCloseArguments.Text = options.CloseArguments;
            checkBoxUseCloseMainWindow.Checked = options.UseCloseMainWindow;
        }

        /// <summary>
        /// 重新加载按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCXJZ重新加载_Click(object sender, EventArgs e)
        {
            LoadDataFromFile();
        }

        /// <summary>
        /// 保存按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonBC保存_Click(object sender, EventArgs e)
        {
            var options = ServiceOptions.LoadFromFile();
            options.ServiceName = textBoxServiceName.Text;
            options.DisplayName = textBoxDisplayName.Text;
            options.Description = textBoxDescription.Text;
            options.StartType = comboBoxStartType.Text;
            options.Account = comboBoxAccount.Text;
            options.ExeFileName = textBoxExeFileName.Text;
            options.StartArguments = textBoxStartArguments.Text;
            options.CloseArguments = textBoxCloseArguments.Text;
            options.UseCloseMainWindow = checkBoxUseCloseMainWindow.Checked;
            options.SaveToFile();
        }

        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonQX取消_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// 选择文件按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonXZWJ选择文件_Click(object sender, EventArgs e)
        {
            openFileDialogExe.ShowDialog(this);
        }

        /// <summary>
        /// 打开文件成功事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openFileDialogExe_FileOk(object sender, CancelEventArgs e)
        {
            textBoxExeFileName.Text = openFileDialogExe.FileName;
        }
    }
}
